package com.sjgtw.cloud.auth.config;

import com.sjgtw.cloud.client.oauth.OauthClientConfig;
import com.sjgtw.cloud.client.oauth.builder.api.FouraOAuthApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LoginOauthConfig {

	private Logger log = LoggerFactory.getLogger(LoginOauthConfig.class);
	
    @Bean
    public OauthClientConfig oauthClientConfig(
    		@Value("${oauth.baseUrl}") String baseUrl,
    		@Value("${oauth.clientId}") String clientId,
    		@Value("${oauth.clientSecret}") String clientSecret,
    		@Value("${oauth.redirectUri}") String redirectUri,
    		@Value("${oauth.endsession.endpoint}") String endSessionEndpoint,
    		@Value("${oauth.endsession.redirectUri}") String endSessionRedirectUri,
    		@Value("${oauth.error.endpoint}") String errorEndpoint
    		){
    	
    	log.debug("baseUrl {}",baseUrl);
    	//init
    	FouraOAuthApi.DEFAULT_WEB_URL = baseUrl;
    	
    	OauthClientConfig config = new OauthClientConfig();
    	config.setBaseUrl(baseUrl);
    	config.setClientId(clientId);
    	config.setClientSecret(clientSecret);
    	config.setRedirectUri(redirectUri);
    	config.setEndSessionEndpoint(endSessionEndpoint);
    	config.setEndSessionRedirectUri(endSessionRedirectUri);
    	config.setErrorEndpoint(errorEndpoint);
    	
    	log.info("oauth config {}",config);
    	return config;
    }
}
